/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#include "database/DataBase.h"
#include "httpd/SIndex.h"
#include "ACSConfig.h"

namespace acs
{

std::vector<std::string> SIndex::getHtml(std::string divID, std::string query)
{
	std::stringstream stream;
	std::vector<std::string> result;

	stream << "<table width=\"1024\">";
	stream << "	<tr><td>";

	stream << "		<br/>";

	stream << "		<div style=\"overflow: hidden; margin-left: 10px;\">";
	stream << "			<span class=\"head1\">Auto Configuration Dashboard</span><br/>";
	stream << "		</div>";

	stream << "		<hr/>";

	stream << "			<span class=\"head2\" style=\"overflow: hidden; margin-left: 20px;\">Last Active Devices</span><br/><br/>";

	stream << "		<div id=\"devicesList\"style=\"overflow: hidden; margin-left: 20px;\">";
	stream << 			SIndex::getDevicesHtml(query);
	stream << "		</div>";

	stream << "		<hr/>";

	stream << "		<div id=\"devicesLog\" style=\"margin-left: 20px;\">";
	stream << 			SIndex::getLogsHtml(0, SC_LOG_LIMIT);
	stream << "		</div>";

	stream << "	</td></tr>";
	stream << "</table>";

	result.push_back(divID);
	result.push_back(stream.str());

	return result;

}

std::vector<std::string> SIndex::getDevices(std::string query)
{
	std::vector<std::string> result;

	result.push_back("devicesList");
	result.push_back(SIndex::getDevicesHtml(query));

	return result;
}

std::vector<std::string> SIndex::getLogs(int offset, int limit)
{
	std::vector<std::string> result;

	result.push_back("devicesLog");
	result.push_back(SIndex::getLogsHtml(offset,limit));

	return result;
}

std::string SIndex::getDevicesHtml(std::string query)
{
	std::stringstream stream;
	std::vector<Device> devices = DataBase::getInstance()->getLastAccessDevices(SC_DEVICE_LIMIT);

	if(devices.empty())
	{
		stream << "<span class=\"text\">No devices available...</span>";
	}
	else
	{
		stream << "		<table width=\"1004\">";
		stream << "		<tr class=\"tableRowHeader\">";
		stream << "			<td>Name</td><td>Release</td><td>Ip</td><td>Configuration</td><td>Last Contact On</td><td>License</td><td>Customer</td>";
		stream << "		</tr>";
		for(std::vector<Device>::iterator itr=devices.begin(); itr!=devices.end(); itr++)
		{
			stream << "		<tr cellpadding=\"0\" cellspacing=\"0\" class=\"tableRow\" onmouseover=\"javascript: document.body.style.cursor = 'pointer';\" onmouseout=\"javascript: document.body.style.cursor = 'default';\" onclick=\"javascript: window.location = '/devices.html#show#" << itr->getID() << "'; document.body.style.cursor = 'default';\">";
			if(itr->getConfigID() == 0)
				stream << "			<td>" << itr->getStatusIcon() << "&nbsp;&nbsp;" << itr->getName() << "</td><td>" << itr->getRelease() << "</td><td>" << (itr->getIp().empty() ? "not online" : itr->getIp()) << "</td><td>none</td><td>" << (itr->getContacted() == 0 ? "never" : rgcpp::ToString(itr->getContacted(),"%Y-%m-%d %X")) << "</td><td>" << itr->getLicenseID() << "</td><td>" << itr->getCustomerName() << "</td>";
			else
				stream << "			<td>" << itr->getStatusIcon() << "&nbsp;&nbsp;" << itr->getName() << "</td><td>" << itr->getRelease() << "</td><td>" << (itr->getIp().empty() ? "not online" : itr->getIp()) << "</td><td>" << DataBase::getInstance()->getConfiguration(itr->getConfigID()).getName() << "</td><td>" << (itr->getContacted() == 0 ? "never" : rgcpp::ToString(itr->getContacted(),"%Y-%m-%d %X")) << "</td><td>" << itr->getLicenseID() << "</td><td>" << itr->getCustomerName() << "</td>";
			stream << "		</tr>";
		}
		stream << "		</table>";
	}

	return stream.str();
}

std::string SIndex::getLogsHtml(int offset, int limit)
{
	std::stringstream stream;
	stream << "			<span class=\"head2\">Last Access Logs</span><br/><br/>";

	unsigned int count = DataBase::getInstance()->getLastLogsCount();
	std::vector<Log> logs = DataBase::getInstance()->getLastLogs(ACS_LL_NOTICE,offset,limit);

	if(logs.empty())
	{
		stream << "<span class=\"text\">No logs available...</span>";
	}
	else
	{
		stream << "	<div>";
		stream << "		<table width=\"1004\">";
		stream << "		<tr class=\"tableRowHeader\">";
		stream << "			<td>Log</td><td>Time</td><td>IP</td><td>Release</td><td>Command</td><td>Result</td><td>Message</td>";
		stream << "		</tr>";
		for(std::vector<Log>::iterator itr=logs.begin(); itr!=logs.end(); itr++)
		{
			if(itr->getDeviceID() != 0)
				stream << "		<tr cellpadding=\"0\" cellspacing=\"0\" class=\"tableRow\" onmouseover=\"javascript: document.body.style.cursor = 'pointer';\" onmouseout=\"javascript: document.body.style.cursor = 'default';\" onclick=\"javascript: window.location = '/devices.html#show#" << itr->getDeviceID() << "'; document.body.style.cursor = 'default';\">";
			else
				stream << "		<tr cellpadding=\"0\" cellspacing=\"0\" class=\"tableSimpleRow\" >";

			switch(itr->getLevel())
			{
				case ACS_LL_DEBUG:
				{
					stream << "			<td style=\"width: 80px;\">Trace</td>";
				}break;
				case ACS_LL_NOTICE:
				{
					stream << "			<td style=\"width: 80px;\">Information</td>";
				}break;
				case ACS_LL_ERROR:
				{
					stream << "			<td style=\"width: 80px;\">Error</td>";
				}break;
				default:
				{
					stream << "			<td style=\"width: 80px;\">Warning</td>";
				}break;
			}
			stream << "			<td style=\"width: 150px;\">" << rgcpp::ToString(itr->getDate(),"%Y-%m-%d %X") << "</td><td style=\"width: 110px;\">" << itr->getIp() << "</td><td style=\"width: 120px;\">" << itr->getRelease() << "</td><td>" << itr->getCommand() << "</td><td style=\"width: 100px;\">" << itr->getResult() << "</td><td>" << itr->getMessage() << "</td>";
			stream << "		</tr>";
		}
		stream << "		</table>";
		stream << "	</div>";
	}

	return stream.str();
}


}
